package com.cykj.controller;

import com.cykj.dto.ResponseDTO;
import com.cykj.service.GoodService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GoodController {
    //      @Autowired
//    private RedisTemplate<String,String> redisTemplate;
//@
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private GoodService goodService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
@Value("${server.port}")
private String port;
    @RequestMapping("test")
    public String test() {
        return "hello-good-test "+port;

//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("姓名", "佳佳");
//        jsonObject.put("sex", "boy");
//        jsonObject.put("age", "24");
////        redisTemplate.opsForValue().set("fast", jsonObject);
//        redisTemplate.opsForValue().set("年龄", "90");
//        System.out.println(redisTemplate.opsForValue().get("年龄"));
    }
    @RequestMapping("/hotGoodsToRedis")
    public ResponseDTO hotGoodsToRedis() throws JsonProcessingException {
      return goodService.getHotGoods();
    }
    @RequestMapping("/getHotGood")
    public ResponseDTO getHotGood(int id) {

        return goodService.getHotGood(id);
    }
    @RequestMapping("/getHotGoodList")
    public ResponseDTO getHotGoodList(int pageNum, int pageSize) {

        return goodService.getHotGoodList(pageNum,pageSize);
    }
    @RequestMapping("/getGoodClass")
    public ResponseDTO getGoodClass(){

        return goodService.getGoodClass();
    }
    @RequestMapping("/getGoodClassType")
    public ResponseDTO getGoodClassType(@RequestParam String id){

        return goodService.getGoodClassType(id);
    }
    @RequestMapping("/getGoods")
    public ResponseDTO getGoods(@RequestParam String id) throws JsonProcessingException {

        return goodService.getGoods(id);
    }
    @RequestMapping("/sendMsg")
    public String sendMsg() {
  rocketMQTemplate.convertAndSend("test-topic","测试消息");
  return "done";
    }
    @RequestMapping("/goodToRedis")
    public ResponseDTO goodToRedis() {
        return goodService.goodToRedis();
    }
    @RequestMapping("/reduceStorage")
    public ResponseDTO reduceStorage(@RequestParam String id,@RequestParam String num) {
        return goodService.reduceStorage(id,num);
    }
    @RequestMapping("/addStorage")
    public ResponseDTO addStorage(@RequestParam String id,@RequestParam String num) {
        return goodService.addStorage(id,num);
    }
    @RequestMapping("/generateOrder")
    public ResponseDTO generateOrder(@RequestParam String good_id,@RequestParam String user_id,@RequestParam String pay_state,@RequestParam String buy_count,@RequestParam String is_time_out) {
        return goodService.generateOrder(good_id,user_id,pay_state,buy_count,is_time_out);
    }

}